const express = require("express");

const pool = require("../pool.js");

const r = express.Router();

// 蛋糕接口
r.get("/cake_type", (req, res) => {
  // 蛋糕分类标题
  let cakeTypeSql = "select * from cake_type;";
  pool.query(cakeTypeSql, (err, cakeType_result) => {
    if (err) throw err;
    // console.log(cakeType_result);
    res.send({
      code: 200,
      msg: "ok",
      cakeType: cakeType_result,
    });
  });
});

// 查询所有蛋糕
r.get("/allCake", (req, res) => {
  let allCakesSql =
    "SELECT c_id,cake_title,sold_count,price,cake_img from cake_type,cake where cake_type.t_id=cake.c_type_id ORDER BY `cake`.`c_id`;";
  pool.query(allCakesSql, (err, allCakeresult) => {
    if (err) throw err;
    res.send({ code: 200, msg: "ok", allCake: allCakeresult });
  });
});

// 按分类查蛋糕的接口
r.get("/selCakes", (req, res) => {
  let selTid = req.query.tid;
  let cakesSql =
    "SELECT c_id,cake_title,sold_count,price,cake_img from cake_type,cake where cake_type.t_id=cake.c_type_id and cake_type.t_id=?;";
  pool.query(cakesSql, [selTid], (err, selCakeresult) => {
    if (err) throw err;
    // console.log(selCakeresult);
    if (selCakeresult.length == 0) {
      res.send({ code: 201, msg: "no found cake" });
    } else {
      res.send({ code: 200, msg: "ok", selCake: selCakeresult });
    }
  });
});

// 对应的蛋糕详情接口
r.get("/cakeDetails", (req, res) => {
  var selCid = req.query.cid;
  // 蛋糕详情
  let allCakeDetailSql = "SELECT * FROM cake where cake.c_id=?;";
  pool.query(allCakeDetailSql, [selCid], (err, allCakeDetailsResult) => {
    if (err) throw err;
    // console.log(allCakeDetailsResult);
    if (allCakeDetailsResult.length == 0) {
      res.send({ code: 201, msg: "no found cakeDetails" });
    } else {
      // 蛋糕轮播图
      let cakeDetailsBannerSql =
        "SELECT cc_id,cc_pic from cake_carousel_img,cake WHERE cake_carousel_img.carousel_id=cake.c_id AND cake.c_id=?";
      pool.query(cakeDetailsBannerSql, [selCid], (err, cakeBannerResult) => {
        if (err) throw err;
        if (cakeBannerResult.length == 0) {
          res.send({ code: 201, msg: "no found cakeBanners" });
        } else {
          // 蛋糕尺寸
          let cakeSizeSql =
            "SELECT s_id,size_tname from cake_size,cake WHERE cake_size.cake_size_id=cake.c_id AND cake.c_id=?;";
          pool.query(cakeSizeSql, [selCid], (err, cakeSizeResult) => {
            if (err) throw err;
            if (cakeSizeResult.length == 0) {
              res.send({ code: 201, msg: "no found cakeSizes" });
            } else {
              // 蛋糕配方
              let cakeCompositionSql =
                "SELECT co_id,composition_name,is_empty from cake_composition,cake WHERE cake_composition.cake_composition_id=cake.c_id AND cake.c_id=?;";
              pool.query(
                cakeCompositionSql,
                [selCid],
                (err, cakeCompositionResult) => {
                  if (err) throw err;
                  if (cakeCompositionResult.length == 0) {
                    res.send({ code: 201, msg: "no found cakeCompositions" });
                  } else {
                    // 蛋糕夹心
                    let cakeSandwichSql =
                      "SELECT sw_id,sw_name,is_empty from cake_sandwich,cake WHERE cake_sandwich.cake_sandwich_id=cake.c_id AND cake.c_id=?;";
                    pool.query(
                      cakeSandwichSql,
                      [selCid],
                      (err, cakeSandwichResult) => {
                        if (err) throw err;
                        if (cakeSandwichResult.length == 0) {
                          res.send({
                            code: 201,
                            msg: "no found cakeSandwichs",
                          });
                        } else {
                          // 蛋糕详情图片
                          let cakeDetailImgSql =
                            "SELECT cd_id,cd_pic FROM cake_details_img,cake WHERE cake_details_img.details_id=cake.c_id AND cake.c_id=?";
                          pool.query(
                            cakeDetailImgSql,
                            [selCid],
                            (err, cakeDetailImgResult) => {
                              if(err) throw err;
                              if(cakeDetailImgResult.length==0){
                                res.send({
                                  code: 201,
                                  msg: "no found cakeDetailImgs",
                                });
                              }else{
                                res.send({
                                  code: 200,
                                  msg: "ok",
                                  allCakeDetailsResult: allCakeDetailsResult,
                                  cakeBannerResult: cakeBannerResult,
                                  cakeSizeResult: cakeSizeResult,
                                  cakeCompositionResult: cakeCompositionResult,
                                  cakeSandwichResult: cakeSandwichResult,
                                  cakeDetailImgResult,
                                  cakeDetailImgResult
                                });
                              }
                            }
                          );
                        }
                      }
                    );
                  }
                }
              );
            }
          });
        }
      });
    }
  });
});

module.exports = r;
